package com.zl.smartmall.library.po;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Property;
import net.tsz.afinal.annotation.sqlite.Table;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@Table(name = "tb_ParticipationInfo")
public class ParticipationInfo {

	@Id
	private int id;
	@Property
	private int particationId;
	@Property
	private int type;
	@Property
	private long dateline;
	@Property
	private String content;
	@Property
	private String linkData;
	@Property
	private String picture;
	@Property
	private String title;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getParticationId() {
		return particationId;
	}

	public void setParticationId(int particationId) {
		this.particationId = particationId;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public long getDateline() {
		return dateline;
	}

	public void setDateline(long dateline) {
		this.dateline = dateline;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getLinkData() {
		return linkData;
	}

	public void setLinkData(String linkData) {
		this.linkData = linkData;
	}

	public String getPicture() {
		return picture;
	}

	public void setPicture(String picture) {
		this.picture = picture;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public static List<ParticipationInfo> parse(JSONArray jsonArray) throws JSONException {
		List<ParticipationInfo> participationInfos = new ArrayList<ParticipationInfo>();
		for(int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			if(jsonObject.length() > 0) {
				ParticipationInfo participationInfo = new ParticipationInfo();
				participationInfo.content = jsonObject.getString("content");
				participationInfo.dateline = jsonObject.getLong("dateline");
				participationInfo.particationId = jsonObject.getInt("id");
				participationInfo.picture = jsonObject.getString("pic");
				participationInfo.title = jsonObject.getString("title");
				participationInfo.type = jsonObject.getInt("mtype");
				if(participationInfo.type == 2) {//网页
					participationInfo.linkData = jsonObject.getString("linkdata");
				
				}else if(participationInfo.type == 3) {//商品列表
					StringBuilder sb = new StringBuilder();
					sb.append(jsonObject.getJSONObject("linkdata").getInt("actid") + "|");
					sb.append(jsonObject.getString("name"));
					participationInfo.linkData = sb.toString();
					
				}else if(participationInfo.type == 4) {//商品详情
					StringBuilder sb = new StringBuilder();
					sb.append(jsonObject.getJSONObject("linkdata").getInt("shop_id") + "|");
					sb.append(jsonObject.getJSONObject("linkdata").getInt("product_id"));
					participationInfo.linkData = sb.toString();					
				}
				participationInfos.add(participationInfo);
			}
		}
		
		return participationInfos;
	}
	
}
